console.log('main');

const delay = async (time) => {
    return new Promise(resolve => {
        setTimeout(resolve, time);
    })
};

const main = async () => {
    const world = new World();

    const collisionSystem =  world.createSystem(CollisionSystem);
    const playerControllerSystem = world.createSystem(PlayerControllerSystem);
    const ballAiSystem = world.createSystem(BallAiSystem);
    const ruleSystem = world.createSystem(RuleSystem);
    const scoreAnimationSystem = world.createSystem(ScoreAnimationSystem);
    const renderSystem = world.createSystem(RenderSystem);

    const fps = 60;
    const frameTime = 1000 / fps;
    let prevFrameStart = new Date().valueOf();

    while (true) {
        const frameStart = new Date().valueOf();
        time.delta = frameStart - prevFrameStart;

        playerControllerSystem.execute();
        ballAiSystem.execute();
        collisionSystem.execute();
        scoreAnimationSystem.execute();
        ruleSystem.execute();
        renderSystem.execute();

        const frameEnd = new Date().valueOf();
        await delay(Math.max(0, Math.floor(frameTime - (frameEnd - frameStart)) - 2));

        prevFrameStart = frameStart;
    }
};

main();